package com.yuanda.wordhot.service;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yuanda.wordhot.entity.ResultEntity;
import com.yuanda.wordhot.entity.UserEntity;
import com.yuanda.wordhot.repository.UserRepository;

@Service
public class UserService {
	@Autowired
	private UserRepository  userRepository;
	
	private final static Logger logger = LoggerFactory.getLogger(UserService.class);
	
	public UserEntity getCurrentUser() {
		logger.debug("获取当前登录用户");
		final Long currentUserId = (Long) SecurityUtils.getSubject()
				.getPrincipal();
		if (currentUserId != null) {
			return getUser(currentUserId);
		} else {
			return null;
		}
	}

	public void createUser(String username, String email, String password) {
		//UserEntity user = new UserEntity();
	}

	public ResultEntity getAllUsers(int start, int count) {
		List<UserEntity> rows = userRepository.getAllUsers(start, count);
		long total = userRepository.getTotalUsers();
		ResultEntity result = new ResultEntity(total, rows);
		return result;
	}

	public UserEntity getUser(Long userId) {
		return userRepository.get(userId);
	}

	@Transactional
	public void deleteUser(Long userId) {
		userRepository.remove(userRepository.get(userId));		
	}
	
	@Transactional
	public void updateUser(UserEntity user) {
		userRepository.update(user);
	}

	@Transactional
	public void addUser(UserEntity user) {
		userRepository.add(user);
	}
}
